package org.dromara.bykt.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.bykt.domain.bo.BYKTGeographyInfoBo;
import org.dromara.bykt.domain.vo.BYKTGeographyInfoVo;
import org.dromara.bykt.service.IBYKTGeographyInfoService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 省份
 *
 * @author Hou
 * @date 2025-07-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bykt/geographyInfo")
public class BYKTGeographyInfoController extends BaseController {

    private final IBYKTGeographyInfoService geographyInfoService;

    /**
     * 查询省份列表
     */
    @GetMapping("/list")
    public TableDataInfo<BYKTGeographyInfoVo> list(BYKTGeographyInfoBo bo, PageQuery pageQuery) {
        return geographyInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出省份列表
     */
    @Log(title = "省份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BYKTGeographyInfoBo bo, HttpServletResponse response) {
        List<BYKTGeographyInfoVo> list = geographyInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "省份", BYKTGeographyInfoVo.class, response);
    }

    /**
     * 获取省份详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BYKTGeographyInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(geographyInfoService.queryById(id));
    }

    /**
     * 新增省份
     */
    @Log(title = "省份", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BYKTGeographyInfoBo bo) {
        return toAjax(geographyInfoService.insertByBo(bo));
    }

    /**
     * 修改省份
     */
    @Log(title = "省份", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BYKTGeographyInfoBo bo) {
        return toAjax(geographyInfoService.updateByBo(bo));
    }

    /**
     * 删除省份
     *
     * @param ids 主键串
     */
    @Log(title = "省份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geographyInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 查询省份下拉
     *
     * @return
     */
    @GetMapping("/queryListAll")
    public R<List<BYKTGeographyInfoVo>> queryListAll() {
        return R.ok(geographyInfoService.queryListAll());
    }
}
